package net.feling.mcloud.utils;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class xmlHandler extends DefaultHandler {

    private List<HashMap<String, String>> mapList = null;
    private HashMap<String, String> map = null;
    private String currentTag = null;
    private String currentValue = null;
    private String nodeName = null;

    public xmlHandler(String _nodeName) {
        this.nodeName = _nodeName;
    }

    public List<HashMap<String, String>> getMapList() {
        return mapList;
    }

    @Override
    public void startDocument() throws SAXException {
        mapList = new ArrayList<HashMap<String, String>>();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if (qName.equals(nodeName)) {
            map = new HashMap<String, String>();
        }
        if (attributes != null && map != null) {
            for (int i = 0; i < attributes.getLength(); i++) {
                map.put(attributes.getQName(i), attributes.getValue(i));
            }
        }
        currentTag = qName;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (currentTag != null && currentValue != null) {
            map.put(currentTag, currentValue.trim());
            currentTag = null;
            currentValue = null;
        }
        if (qName.equals(nodeName)) {
            mapList.add(map);
            map = null;
        }
    }

    /**
     *
     * @param ch 字符流
     * @param start 開支位置
     * @param length 結束位置
     * @throws SAXException 解析異常
     */
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String sch = new String(ch, start, length);
        if (currentValue == null) {
            currentValue = sch;
        } else {
            currentValue = currentValue + sch;
        }
    }

    /**
     * 读取xml配置文件
     *
     * @param xmlString 配置文件的路径
     * @return 成功返回 包含改文件内所有配置的map对象，失败返回null
     */
    public static List<HashMap<String, String>> execute(String xmlString, String _nodeName) {
        try {
            InputStream in = new ByteArrayInputStream(xmlString.getBytes("UTF8"));
            SAXParser saxparser = SAXParserFactory.newInstance().newSAXParser();
            xmlHandler xmlhadler = new xmlHandler(_nodeName);
            // saxparser.parse(new File(Path_xmlfile), xmlhadler);
            saxparser.parse(in, xmlhadler);
            return xmlhadler.getMapList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 读取xml配置文件
     *
     * @param _nodeName 配置文件的路径
     * @return 成功返回 包含改文件内所有配置的map对象，失败返回null
     */
    public static List<HashMap<String, String>> execute(InputStream in, String _nodeName) {
        try {
            SAXParser saxparser = SAXParserFactory.newInstance().newSAXParser();
            xmlHandler xmlhadler = new xmlHandler(_nodeName);
            // saxparser.parse(new File(Path_xmlfile), xmlhadler);
            saxparser.parse(in, xmlhadler);
            return xmlhadler.getMapList();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
